package com.diaohw.module.metadata.service.meta;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.diaohw.module.metadata.mapper.MetaVersionMapper;
import com.diaohw.module.metadata.obj.dto.MetaVersionDTO;
import com.diaohw.module.metadata.obj.entity.MetaVersionDO;
import com.diaohw.module.metadata.obj.query.MetaVersionQuery;
import com.diaohw.module.metadata.obj.vo.MetaVersionVO;
import com.diaohw.module.metadata.service.MetaVersionService;
import com.diaohw.platform.common.obj.page.PageResultVo;
import com.diaohw.platform.framework.db.utils.WrapperUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 元数据版本号 服务实现类
 * </p>
 *
 * @author D-Hw
 * @since 2025-04-07
 */
@Service
public class MetaVersionImpl extends ServiceImpl<MetaVersionMapper, MetaVersionDO> implements MetaVersionService {

    @Override
    public MetaVersionVO create(MetaVersionDTO data) {
        MetaVersionDO model = INSTANCE.dtoToEntity(data);
        this.save(model);
        return INSTANCE.entityToVo(model);
    }

    @Override
    public MetaVersionVO update(String id, MetaVersionDTO data) {
        MetaVersionDO model = INSTANCE.dtoToEntity(data);
        model.setId(id);
        this.updateById(model);
        return INSTANCE.entityToVo(model);
    }

    @Override
    public Boolean delete(String id) {
        return this.removeById(id);
    }

    @Override
    public MetaVersionVO detail(String id) {
        MetaVersionDO model = this.getById(id);
        return INSTANCE.entityToVo(model);
    }

    @Override
    public List<MetaVersionVO> findByList(MetaVersionQuery query) {
        LambdaQueryWrapper<MetaVersionDO> wrapper = createWrapper(query);
        List<MetaVersionDO> list = this.list(wrapper);
        return INSTANCE.entityToVo(list);
    }

    @Override
    public PageResultVo<MetaVersionVO> findByPage(MetaVersionQuery query) {
        LambdaQueryWrapper<MetaVersionDO> wrapper = createWrapper(query);
        Page<MetaVersionDO> page = this.page(WrapperUtils.createPage(query), wrapper);
        List<MetaVersionVO> list = INSTANCE.entityToVo(page.getRecords());
        return PageResultVo.create(page.getTotal(), list);
    }

    /**
    * 生成查询条件
    *
    * @param query 查询条件
    */
    private LambdaQueryWrapper<MetaVersionDO> createWrapper(MetaVersionQuery query) {
        LambdaQueryWrapper<MetaVersionDO> wrapper = Wrappers.lambdaQuery(MetaVersionDO.class);
        wrapper.like(StrUtil.isNotBlank(query.getTableId()), MetaVersionDO::getTableId, query.getTableId());
        wrapper.like(StrUtil.isNotBlank(query.getTableCode()), MetaVersionDO::getTableCode, query.getTableCode());
        wrapper.like(StrUtil.isNotBlank(query.getTableName()), MetaVersionDO::getTableName, query.getTableName());
//        wrapper.like(StrUtil.isNotBlank(query.getTableData()), MetaVersionDO::getTableData, query.getTableData());
        wrapper.like(StrUtil.isNotBlank(query.getVersion()), MetaVersionDO::getVersion, query.getVersion());
        return wrapper;
    }

}
